<script setup lang="ts">
import { onMounted, ref, computed } from 'vue';
// 导入类型
import type { FirstDept } from '@/types/consult';
// 获取诊室
import { getAllDept } from '@/services/consult';
// 导入store
import { useConsultStore } from '@/stores/consult';

// 创建store实例
const consultStore = useConsultStore();

// 左侧一级导航栏选中索引
const active = ref(0);
// 诊室列表
const list = ref<FirstDept[]>([]);

onMounted(async () => {
  // 获取诊室数据
  const data = await getAllDept();
  list.value = data;
  console.log(list.value);
});
// 根据选中的一级诊室数据计算得出
const subDept = computed(() => {
  return list.value[active.value]?.child;
});
</script>

<template>
  <div class="consult-dept">
    <CpNavBar title="选择科室" />
    <div class="wrapper">
      <van-sidebar v-model="active">
        <van-sidebar-item v-for="item in list" :title="item.name" :key="item.id" />
      </van-sidebar>
      <div class="sub-dept">
        <router-link
          to="/consult/illness"
          v-for="sub in subDept"
          :key="sub.id"
          @click="consultStore.setDept(sub.id)"
        >
          {{ sub.name }}
        </router-link>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.consult-dept {
  padding-top: 0px;

  .wrapper {
    height: calc(100vh - 46px);
    // overflow: hidden;
    display: flex;

    .van-sidebar {
      width: 114px;
      &-item {
        padding: 14px;
        color: var(--cp-tag);
        &--select {
          color: var(--cp-main);
          font-weight: normal;
          &::before {
            display: none;
          }
        }
      }
    }

    .sub-dept {
      // flex: 1;
      // height: 100%;
      width: 100%;
      overflow-y: auto;
      > a {
        display: block;
        padding: 14px 30px;
        color: var(--cp-dark);
      }
    }
  }
}
</style>
